package com.db.williamchartdemo;

import android.graphics.Color;
import android.os.Bundle;
import com.db.williamchart.extensions.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.db.williamchart.slidertooltip.SliderTooltip;
import com.db.williamchart.view.AxisChartView;
import com.db.williamchartdemo.databinding.DemoFragmentBinding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoFragment extends Fragment {

    private DemoFragmentBinding binding;
    private final long animationDuration = 1000L;

    private List<Pair<String, Float>> lineSet = Arrays.asList(
            new Pair<>("label1", 5f),
            new Pair<>("label2", 4.5f),
            new Pair<>("label3", 4.7f),
            new Pair<>("label4", 3.5f),
            new Pair<>("label5", 3.6f),
            new Pair<>("label6", 7.5f),
            new Pair<>("label7", 7.5f),
            new Pair<>("label8", 10f),
            new Pair<>("label9", 5f),
            new Pair<>("label10", 6.5f),
            new Pair<>("label11", 3f),
            new Pair<>("label12", 4f)
    );
    private List<Pair<String, Float>> barSet = Arrays.asList(
            new Pair<String, Float>("JAN", 4F),
            new Pair<String, Float>("FEB", 7F),
            new Pair<String, Float>("MAR", 2F),
            new Pair<String, Float>("MAY", 2.3F),
            new Pair<String, Float>("APR", 5F),
            new Pair<String, Float>("JUN", 4F)
    );
    private List<Pair<String, Float>> horizontalBarSet = Arrays.asList(
            new Pair<String, Float>("PORRO", 5F),
            new Pair<String, Float>("FUSCE", 6.4F),
            new Pair<String, Float>("EGET", 3F)
    );
    private List<Float> donutSet = Arrays.asList(20f, 80f, 100f);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DemoFragmentBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        /**
         * Line Chart
         */
        binding.lineChart.setGradientFillColors(Arrays.asList(
                Color.parseColor("#81FFFFFF"),
                Color.TRANSPARENT));
        binding.lineChart.getChartAnimation().setDuration(animationDuration);
        SliderTooltip sliderTooltip = new SliderTooltip();
        sliderTooltip.setColor(Color.WHITE);
        binding.lineChart.setTooltip(sliderTooltip);
        binding.lineChart.setOnDataPointTouchListener(new AxisChartView.OnDataPointTouchListener() {
            @Override
            public void onPointTouch(int index, float x, float y) {
                String label = lineSet.get(index).second.toString();
                binding.lineChartValue.setText(label);
            }
        });

        binding.lineChart.animate(lineSet);

        /**
         * Bar Chart
         */
        binding.barChart.getChartAnimation().setDuration(animationDuration);
        binding.barChart.animate(barSet);

        /**
         * Donut Chart
         */
        binding.donutChart.setDonutColors(
                Arrays.asList(
                        Color.parseColor("#FFFFFF"),
                        Color.parseColor("#9EFFFFFF"),
                        Color.parseColor("#8DFFFFFF")
                )
        );
        binding.donutChart.getChartAnimation().setDuration(animationDuration);
        binding.donutChart.animate(donutSet);

        /**
         * Horizontal Bar Chart
         */
        binding.horizontalBarChart.getChartAnimation().setDuration(animationDuration);
        binding.horizontalBarChart.animate(horizontalBarSet);
    }
}
